<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/config.inc.php');
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/top.inc.php');
?>


<?php
$building_id = mysql_real_escape_string(strip_tags($_GET['bid']));
$instructor_id = mysql_real_escape_string(strip_tags($_GET['iid']));

$get_building_data = mysql_query("SELECT * FROM buildings WHERE id = $building_id");
$building_name = mysql_result($get_building_data,0,'building_name');
$abbreviation = mysql_result($get_building_data,0,'abbreviation');

$query = "SELECT * FROM instructors WHERE id = '$instructor_id'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$fname = $row['first_name'];
$lname = $row['last_name'];

if (isset($_POST['submit_pref']))
{
	$timeslot_id = mysql_real_escape_string(strip_tags($_POST['timeslot']));
	
	$query = "SELECT MAX(rank_of_preference) AS max_rank
			  FROM time_slot_preferences
			  WHERE instructor_id = '$instructor_id'";
	$result = mysql_query($query);
	$row = mysql_fetch_assoc($result);
	$rank = $row['max_rank'] + 1;
	
	echo "<h1>$fname $lname Timeslot Preferences for $abbreviation</h1>";
	
	$query = "INSERT INTO time_slot_preferences (id, instructor_id, time_slot_id, rank_of_preference)
			  VALUES ('', '$instructor_id', '$timeslot_id', '$rank')";
	if ($result = mysql_query($query))
	{
		echo "<p>Timeslot preference was successfully added</p>";
	}
	else
	{
		echo "<p>Timeslot preference was <b>NOT</b> successfully added</p>";
	}
	echo "<p><a href=\"update_preferences_second.php?iid=$instructor_id&amp;bid=$building_id\">&lt;&lt; Back to Preferences</a></p>";
}
else
{
	echo "<p><a href=\"update_preferences_second.php?iid=$instructor_id&amp;bid=$building_id\">&lt;&lt; Back to Preferences</a></p>";
	echo "<h1>$fname $lname Timeslot Preferences for $abbreviation</h1>";
	?>
	
	<form name="add_pref" action="add_pref.php<?php echo "?iid=$instructor_id&amp;bid=$building_id"; ?>" method="post">
	<p>Select the timeslot to add to the instructor's preferences:</p>
	<p><select name="timeslot">
	
	<?php
	//select statement
	$query="SELECT *
			FROM time_slots
			WHERE building_id = '$building_id'
			AND id NOT IN (SELECT time_slot_id FROM time_slot_preferences WHERE instructor_id = '$instructor_id')
			ORDER BY hours_per_week DESC, Monday DESC, Tuesday DESC, Wednesday DESC, Thursday DESC, Friday DESC, Saturday DESC, Sunday DESC, start_time ASC, end_time ASC";
	$result=mysql_query($query);
	while ($row = mysql_fetch_assoc($result))
	{
		echo "<option value=\"".$row['id']."\">";
		$days_of_week = '';
		if ($row['Monday'])
			$days_of_week .= 'M,';
		if ($row['Tuesday'])
			$days_of_week .= 'Tu,';
		if ($row['Wednesday'])
			$days_of_week .= 'W,';
		if ($row['Thursday'])
			$days_of_week .= 'Th,';
		if ($row['Friday'])
			$days_of_week .= 'F,';
		if ($row['Saturday'])
			$days_of_week .= 'Sa,';
		if ($row['Sunday'])
			$days_of_week .= 'Su,';
		$days_of_week = substr_replace($days_of_week, '', -1, 1);
		echo "$days_of_week ".date('g:ia', strtotime($row['start_time']))." - ".date('g:ia', strtotime($row['end_time']))." (".$row['hours_per_week']." hours)</option>";
	}
	?>
	
	</select></p>
	
	<p><input type="submit" name="submit_pref"/></p>
	
	</form>

<?php
}?>

<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/bottom.inc.php');
?>